package com.tzq.SERVLET;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tzq.CLASS.MD5;
import com.tzq.JAVABEAN.ConnectDB;

/**
 * Servlet implementation class LoginServlet
 */
//@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int method;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
    }
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		method = Integer.parseInt(request.getParameter("method"));
		System.out.println(method);
		if(method ==0){
			try {
				checkUser(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(method == 1){
			logoutUser(request,response);
		}
	}

	/**
	 * 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void checkUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		PrintWriter out = response.getWriter();
		String name = request.getParameter("UserName");
		String password =request.getParameter("UserPassword");
		System.out.println(name);
		System.out.println(password);
		HttpSession session = request.getSession();
		ConnectDB conn = new ConnectDB();
		MD5 md5 = new MD5();
		password = md5.getMD5ofStr(password);
		boolean myBean = conn.getConsumerForm(name, password);
		System.out.println(11111);
		if(myBean==false){
			System.out.println(2222);
			out.print("password error");
			return;
		}else{
			ConnectDB connect = new ConnectDB();
			String[] sqlparam = {name};
			ResultSet rsUserID=connect.executeQuery("select UserID from Userinfo where UserName=?",sqlparam );
			rsUserID.next();
			String userID = rsUserID.getString("UserID");
			session.setAttribute("pid",userID );
			session.setAttribute("username", name);
			String refuri = request.getParameter("refuri");
			System.out.println(refuri);
			if(refuri.equals("1")){
				refuri = "/Bys-Website/PersonMainPage.jsp?userID="+userID+"";
			}
			if(refuri.equals("http://localhost/")){
				refuri="/Bys-Website/PersonMainPage.jsp?userID="+userID+"";
			}
			out.print(refuri);
			//out.print(session.getAttribute("pid").toString());
			//response.sendRedirect(refuri);
			return;
		}
	}
	
	public void logoutUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		session.invalidate();
		out.print("<script language=javascript>window.location.href='"+request.getHeader("Referer")+"';</script>");
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
